import React from 'react';
import classNames from 'classnames';
import { Link, History } from 'react-router';
import {
  Modal,
  Button
} from 'react-bootstrap';

import { default as DialogSignDebt } from './dialog/DialogSignDebt';
import { default as DialogSignPoor } from './dialog/DialogSignPoor';
import { default as DialogSignFull } from './dialog/DialogSignFull';
import { default as DialogSignLimit } from './dialog/DialogSignLimit';
import { default as DialogSignNormal } from './dialog/DialogSignNormal';
import Notify from '../common/Notification';
import { product as ProductConfig } from '../config/Common';

class DialogSign extends React.Component {
  constructor() {
    super();

    this.chkProtocl = true;
  }

  static propTypes = {
    product : React.PropTypes.object,
    user : React.PropTypes.object
  }

  static defaultProps = {
    
  }

  handleProtocolChange(value){
    this.chkProtocl = value;
  }
  
  getDialogComponent(){

    let { product, user, history, productAction, onHide } = this.props;

    let dialogHeader, dialogContent, dialogButton;

    if (!product || !user) {
      return {dialogHeader, dialogContent, dialogButton};
    }

    let userLeftCash = parseInt(user.leftcash);
    //积分为负
    if (userLeftCash < 0) {
      dialogHeader = `积分为负，不可进行关注`;
      dialogContent = <DialogSignDebt user={ user }/>;
      dialogButton = [{
         text : "马上充值",
         style : "info",
         callback : function(){
            onHide();
            history.pushState(null, "/Recharge");
         }
      },{
         text : "知道了",
         style : "default"
      }];

      return {dialogHeader, dialogContent, dialogButton};
    }

    //======================================================================

    //积分小于短线关注限制
    if (userLeftCash < ProductConfig.shortSignPoint) {
      dialogHeader = `积分不足，不可进行关注`;
      dialogContent = <DialogSignPoor />;
      dialogButton = [{
         text : "马上充值",
         style : "info",
         callback : function(){
            onHide();
            history.pushState(null, "/Recharge");
         }
      },{
         text : "知道了",
         style : "default"
      }];

      return {dialogHeader, dialogContent, dialogButton};
    }

    //======================================================================

    let productLimit = parseInt(product.limit),
        productViewCount = parseInt(product.viewcount);

    //策略关注数量已达上限
    if(productViewCount >= productLimit){
      dialogHeader = `该策略名额已满`;
      dialogContent = <DialogSignFull />;
      dialogButton = [{
         text : "知道了"
      }];

      return {dialogHeader, dialogContent, dialogButton};
    }

    //======================================================================

    let userViewCount = parseInt(user.shortviewcount);

    //用户关注数量已达上限
    if(userViewCount >= ProductConfig.shortSignLimit){
      dialogHeader = `您关注策略数已满`;
      dialogContent = <DialogSignLimit />;
      dialogButton = [{
         text : "确定"
      }];

      return {dialogHeader, dialogContent, dialogButton};
    }

    //======================================================================

    dialogHeader = `您确认关注策略：${product.name}?`;
    dialogContent = <DialogSignNormal handleProtocolChange = { this.handleProtocolChange.bind(this) }/>;
    dialogButton = [{
         text : "确认",
         style : "info",
         callback : function(){
            if (this.chkProtocl === true) {
              onHide();
              setTimeout(function(){
                productAction.SignProduct(product.gsid);
              }.bind(this), 100);
            }else{
              Notify.makeNotify("请勾选使用协议");
            }
         }
      },{
         text : "取消",
         style : "default"
    }];

    return {dialogHeader, dialogContent, dialogButton};
  }

  render() {

    let { onHide,  visiable, ...other} = this.props;

    let { dialogHeader, dialogContent, dialogButton } = this.getDialogComponent();

    return (
        <Modal show={visiable} onHide = { onHide } {...other}>
          <Modal.Header>
            <Modal.Title>
                { dialogHeader }
            </Modal.Title>
          </Modal.Header>
          <Modal.Body>
            {dialogContent}
          </Modal.Body>
          <Modal.Footer>

            { dialogButton && dialogButton.map((button, index) => {
                let callback = (button.callback || onHide).bind(this);
                let bzStyle = button.style || "info";
                return (
                  <Button onClick={ callback } bsStyle={ bzStyle } key={index}>
                    { button.text }
                  </Button>  
                );
            })}
            
          </Modal.Footer>
        </Modal>
    );
  }
}

export default DialogSign;
